<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Item extends MY_Luckydog {
    public function __construct()
    {

        parent::__construct();
        $this->load->model('luckydog/Item_model', 'item');
        $this->load->model('luckydog/category_model', 'cat');
        
    }
	public function index()
	{
        $_SESSION['v_menu'] = 'item';
        $itemlist = $this->item->get_item_list();
        $data=array(
            'item_list'=>$itemlist,
        );

		$this->load->view('luckydog/item/item_list',$data);
	}
    //添加分类 html
	public function add()
	{
        $catList = $this->cat->getCatList();
        $companyid  = $this->input->get("companyid");
        $itemdetail = $this->item->get_item_by_id($itemid);
        $data=array(
            'cat_list'=>$catList,
            'companyInfo'=>$companyInfo,
        );
		$this->load->view('luckydog/item/itemext',$data);
	}
    //修改分类 html
	public function edit()
	{
        $itemid  = $this->input->get("itemid");
        $catList = $this->cat->getCatList();
	    if (empty($itemid)){
            show_message("参数错误~, <a class='poi' onclick='javascript:history.go(-1);'>返回>></a>");
            exit;
	    }
        $itemdetail = $this->item->get_item_by_id($itemid);
        $itemdetail['companyName'] = $companyInfo['name'];
        $item_img_list = $this->item->get_img_list_byid($itemid);
        $data=array(
            'cat_list'=>$catList,
            'itemdetail'=>$itemdetail,
            'item_img_list'=>$item_img_list,
        );
		$this->load->view('luckydog/item/itemext',$data);
	}
    public function do_add(){
        $categoryid   = $this->input->post("categoryid");
        $title   = $this->input->post("title");
        $summary   = $this->input->post("summary");
        $historystory = $this->input->post("historystory");
        $original_arr = $this->input->post("original_arr");
        $default_arr = $this->input->post("default_arr");
        $referPrice = $this->input->post("referPrice");

        $data = array(
            "categoryid"=>$categoryid,
            "title"=>$title,
            "summary"=>$summary,
            "historystory"=>$historystory,
            "addtime"=>time(),
            "referPrice"=>$referPrice
        );
        $insert_id = $this->item->add_item($data);
        //添加图书背景图片
        $status = 0;
        if($insert_id && !empty($original_arr)){
            foreach($default_arr as $def){
                if($def == 1){
                    $status = 1;
                }
            }
            if($status != 1){
                $default_arr[0] = 1;
            };
            foreach ($original_arr as $key => $value) {
                $temp = array(
                    "itemid"=>$insert_id,
                    "imgurl"=>$value,
                    "is_default"=>$default_arr[$key]
                    );
                $this->item->add_item_img($temp);
            }
        }
        if($insert_id){
            echo json_encode(array('status'=>1,'message'=>'添加成功'));
        }else{
            echo json_encode(array('status'=>-1,'message'=>'添加失败'));
        }
        exit;
    }
    public function do_edit(){
        $itemid   = $this->input->post("itemid");
        $categoryid   = $this->input->post("categoryid");
        $title   = $this->input->post("title");
        $summary   = $this->input->post("summary");
        $historystory = $this->input->post("historystory");
        $original_arr = $this->input->post("original_arr");
        $default_arr = $this->input->post("default_arr");
        $ids_arr = $this->input->post("ids_arr");
        $referPrice = $this->input->post("referPrice");

        $data = array(
            "categoryid"=>$categoryid,
            "title"=>$title,
            "summary"=>$summary,
            "historystory"=>$historystory,
            "referPrice"=>$referPrice
        );

        if (empty($itemid)){
            echo json_encode(array('status'=>-1,'message'=>'参数错误！'));
            exit;
        }
        $edit_flag = $this->item->edit_item($itemid,$data);


         //添加图书背景图片
        $status = 0;
        if(!empty($original_arr)){
            foreach($default_arr as $def){
                if($def == 1){
                    $status = 1;
                }
            }
            if($status != 1){
                $default_arr[0] = 1;
            };
            foreach ($original_arr as $key => $value) {
                if($ids_arr[$key]){
                    //执行修改
                    $temp = array(
                        "is_default"=>$default_arr[$key]
                    );
                    $res = $this->item->edit_item_img($ids_arr[$key],$temp);
                }else{
                    //执行添加
                    $temp = array(
                        "itemid"=>$itemid,
                        "imgurl"=>$value,
                        "is_default"=>$default_arr[$key]
                    );
                    $this->item->add_item_img($temp);
                }
            }
        }

        if($edit_flag){
            echo json_encode(array('status'=>1,'message'=>'商品修改成功'));
        }else{
            echo json_encode(array('status'=>-2,'message'=>'商品修改失败'));
        }
        exit;
    }
    public function do_del(){
        $itemid  = $this->input->post("itemid");
        $this->item->del_item($itemid);
        echo json_encode(array('status'=>1));
        exit;
    }

    public function upload_cover()
    {
        $this->load->library('Upload');
        $upload = new Upload();
        $file_name =$upload->execute($_FILES['Filedata']);

        $savepath='./upload/ad/'.date('Y-m-d').'/';
        $upload->set_upload_dir($savepath);
        if (!file_exists($savepath)){
            mkdir($savepath);
        }
        if(is_string($file_name))
        {
            $res=json_encode(array('code'=>1,'type'=>0,'file_name'=>$file_name));

            echo $res;
        }
        else
        {
            echo json_encode(array('code'=>-1));
        }
    }

    /**
     * 删除图片
     */
     public function del_pic()
    {

        $this->load->library('upload');
        $imgsource = $this->input->post('imgsource', TRUE) ? $this->input->post('imgsource', TRUE) : '';

        //删除图片文件
        if (!empty($imgsource)) {
            $upload = new Upload();
            $res = $upload->del_file($imgsource);
        }
        //删除数据库数据
        $id = $this->input->post('id', TRUE) ? $this->input->post('id', TRUE):0;
        if($id){
            $res = $this->item->del_attach($id);    
        }
        
        echo $res;

    }
    //编辑上线下，删除状态
    public function editStatus(){
        $itemid = $this->input->post("itemid",true) ? $this->input->post("itemid",true) : 0;
        $onlinestatus = $this->input->post("onlinestatus",true) ? $this->input->post("onlinestatus",true) : "";
        $type = $this->input->post("type",true) ? $this->input->post("type",true) : 0;
        $data = array(
            "itemid"=>$itemid,
            "onlinestatus"=>$onlinestatus,
            "type"=>$type
        );
        $res =$this->item->editStatus($data);

        $id = $this->input->post('id', TRUE) ? $this->input->post('id', TRUE):0;
        $res = $this->item->del_attach($id);
        echo $res;
    }

    public function detail(){
        $itemid = $this->input->get("itemid",true) ? $this->input->get("itemid",true) : 0;
        $itemdetail = $this->item->get_item_by_id($itemid);
        $companyInfo = $this->company->get_company_by_id($itemdetail['companyid']);
        $itemdetail['companyName'] = $companyInfo['name'];
        $data=array(
            'itemdetail'=>$itemdetail
        );
        $this->load->view('luckydog/item/item_detail',$data);
    }

}

/* End of file category.php */
/* Location: ./application/controllers/luckydog/category.php */